<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function _initialize(){
        header("content-type:text/html;charset=utf-8");
    }
    
    public function index(){
       // $this->assign('username','伊尔');
        //$this->display();
       /* $i = time();
        echo $i . "<br/>";
        
        echo date("Y-m-d H:i:s",$i);
         
         exit;
         */    
       /* $wish = array(
           'username' => "伊尔",
            'num' => "NO.00001",
            'content' => "愿你每天好心情！",
            'time_at' => "1488942805",
            'color' => '5'
        );
        $this->assign($wish);
        */
      /*  $wishlist =array(
            array(
                    'username' => "伊尔",
                    'num' => "NO.00001",
                    'content' => "愿你每天好心情！",
                     'time_at' => "1488942805",
                     'color' => '1'
                   ),
            array(
                    'username' => "鑫鑫",
                    'num' => "NO.00002",
                    'content' => "“平平安安，健健康康，快快乐乐”这大概是我们心中最朴实的一个心愿吧。！",
                     'time_at' => "1488942805",
                     'color' => '2'
                   ),
            array(
                    'username' => "玲珑",
                    'num' => "NO.00003",
                    'content' => "新的一年，希望家人平安，身边的人过得开心。",
                     'time_at' => "1488942805",
                     'color' => '3'
                   ),
                     );
       * 
       */
        $db = M('wishlist');  // 用表名来实例化一个模型对象
    	$wishlist = $db->select(); // select * from wishlist
        //p($wishlist);
    	//echo "<pre>";
    	// print_r($wishlist);
    	// exit;
	//$this->assign('wishlist',$wishlist);
        $this->assign('wishlist',$wishlist);
        $this->display();
    }
    public function handler(){
        // 获取用户提交的内容  $_POST[]
    	/*$username = I('post.username','匿名') ;  
         // $_POST['username']/  =左边是用于数据库的变字段名； =右边是表单的变量名
    	$content  = I('post.content','这家伙很懒，什么都没留下');
    	$time_at  = time();  // 取当前时间戳
    	$color    = rand(1,5);*/
    	//echo $username;
    	//echo "<br/>";
    	//echo $content;
        $date = array(
            'username' => I('post.username','匿名'),
            'content'  => I('post.content','笑一笑，十年少！'),
            'time_at'  => time(),
            'color'    => rand(1,5)
        );
        //$db = M('wishlist');   // 构建模型对象
        //dump($db->create($date));
       
    	/*$db->create();         // 标准实例，
    	$db->username = $username;
    	$db->content  = $content;
    	$db->time_at  = $time_at;
    	$db->color    = $color;
         */
        
        
    	$result = $db->add();  // 真正入库。
    	//echo $db->getLastSql();
         //if($result>0){
           //$this->success('操作完成','Index/index',3);
       // }else{
            //$this->error('新增失败');
        // }
        $this->redirect('Index/index','', 1, '页面跳转中...');
    }
    public function test(){
        $username = I('post.username');
        $content = I('post.content');
        /*if($username=="" or $content==""){
            $data['status'] = 0;
            $data['info'] ="用户名或许愿内有不能为空！";
            $this->ajaxReturn($data);
        }
        /*if($username == ''){
            $this -> error("不能够为空");
        }*/
        $data = array(
            'username' => $username,
            'content' => $content,
            'time_at'  => time(),
            'color'    => rand(1,5)
            );
        /*$data = array(
            //'username' => $username,
           'username' => I('post.user','匿名'),
            'content'  => I('post.content','这家伙很懒，什么都没有留下！'),
            'time_at'  => time(),
            'color'    => rand(1,5)
        );*/
        //$db = M('wishlist');
        $db = D('wish');
        $validated = $db->create($data);
        if(!$validated){
            $this->ajaxReturn($db->getError());
        }
       // $db->create($data);
        $id = $db->add();
        $data['id'] = $id;
        $data['time_at'] = date("Y-m-d H:i",$data['time_at']);
        $data['content'] = phiz_replace( $data['content']);//刷新问题
//        p($data);
        //echo json_encode($data);
        $data['status'] = 1;
        $this->ajaxReturn($data);
    }
}